﻿namespace Data.Repository
{
    using System;
    using System.Linq;
	using System.Collections.Generic;

    public class PaymentMethodRepository : IPaymentMethodRepository
    {
        private LinqToECommerceDBDataContext _dbContext;

        #region IPaymentMethodRepository Members

		public int CreateNewPaymentMethod(Data.Entity.PaymentMethod theNewPaymentMethod)
		{
			if(theNewPaymentMethod == null)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_PaymentMethod_Create(theNewPaymentMethod.Name, theNewPaymentMethod.VisibleName, theNewPaymentMethod.Description,
					theNewPaymentMethod.ConfigureTemplatePath, theNewPaymentMethod.UserTemplatePath, theNewPaymentMethod.ClassName, theNewPaymentMethod.SystemKeyword,
					theNewPaymentMethod.IsActive, theNewPaymentMethod.DisplayOrder);
			}

			return result;
		}

		public IList<Data.Entity.PaymentMethod> GetAllPaymentMethod()
		{
			List<ECommerce_GetPaymentMethodResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetPaymentMethod();

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.PaymentMethod>();

				foreach(var pm in lstData)
				{
					var theEntity = new Data.Entity.PaymentMethod()
					{
						ClassName = pm.ClassName,
						ConfigureTemplatePath = pm.ConfigureTemplatePath,
						Description = pm.Description,
						DisplayOrder = pm.DisplayOrder,
						IsActive = pm.IsActive,
						Name = pm.Name,
						PaymentMethodID = pm.PaymentMethodID,
						SystemKeyword = pm.SystemKeyword,
						UserTemplatePath = pm.UserTemplatePath,
						VisibleName = pm.VisibleName
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.PaymentStatus> GetAllPaymentStatus()
		{
			List<ECommerce_GetPaymentStatusResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetPaymentStatus();

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.PaymentStatus>();

				foreach(var ps in lstData)
				{
					var theEntity = new Data.Entity.PaymentStatus()
					{
						Name = ps.Name,
						PaymentStatusID = ps.PaymentStatusID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public int DeletePaymentMethod(int id)
		{
			if(id <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				result = _dbContext.ECommerce_PaymentMethod_Delete(id);
			}

			return result;
		}

		public Data.Entity.PaymentMethod GetThePaymentMethod(int id)
		{
			if(id <= 0)
			{
				return null;
			}

			List<Data.PaymentMethod> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.PaymentMethods.Where(exp => exp.PaymentMethodID == id);

				lstData = data.ToList();
			}

			if (lstData != null && lstData.Count > 0)
			{
				var pm = lstData[0];

				Data.Entity.PaymentMethod result;

				result = new Data.Entity.PaymentMethod()
				{
					ClassName = pm.ClassName,
					ConfigureTemplatePath = pm.ConfigureTemplatePath,
					Description = pm.Description,
					DisplayOrder = pm.DisplayOrder,
					IsActive = pm.IsActive,
					Name = pm.Name,
					PaymentMethodID = pm.PaymentMethodID,
					SystemKeyword = pm.SystemKeyword,
					UserTemplatePath = pm.UserTemplatePath,
					VisibleName = pm.VisibleName
				};

				return result;
			}

			return null;
		}

		public int UpdatePaymentMethod(Data.Entity.PaymentMethod thePaymentMethod)
		{
			if(thePaymentMethod == null || thePaymentMethod.PaymentMethodID <= 0)
			{
				return 0;
			}

			int result = 0;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var dbPaymentMethod = (from pm in _dbContext.PaymentMethods
									   where pm.PaymentMethodID.Equals(thePaymentMethod.PaymentMethodID)
									   select pm).FirstOrDefault();

				if(dbPaymentMethod == null)
				{
					result = 0;
				}
				else
				{
					dbPaymentMethod.ClassName = thePaymentMethod.ClassName;
					dbPaymentMethod.ConfigureTemplatePath = thePaymentMethod.ConfigureTemplatePath;
					dbPaymentMethod.Description = thePaymentMethod.Description;
					dbPaymentMethod.DisplayOrder = thePaymentMethod.DisplayOrder;
					dbPaymentMethod.IsActive = thePaymentMethod.IsActive;
					dbPaymentMethod.Name = thePaymentMethod.Name;
					dbPaymentMethod.SystemKeyword = thePaymentMethod.SystemKeyword;
					dbPaymentMethod.UserTemplatePath = thePaymentMethod.UserTemplatePath;
					dbPaymentMethod.VisibleName = thePaymentMethod.VisibleName;

					_dbContext.SubmitChanges();

					result = 1;
				}
			}

			return result;
		}

		#endregion
	}
}
